package com.yeyks.controller.activity;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.param.PageParam;
import com.yeyks.common.param.activity.PushHandPlanStatParam;
import com.yeyks.common.result.activity.*;
import com.yeyks.common.utils.FormatUtils;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.consumer.service.UserMakerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "v4.3推手计划")
@RestController
@Slf4j
@RequestMapping("/push/hand")
public class PushHandPlanStatController {

    @Autowired
    private UserMakerService userMakerService;

    @ApiOperation(value = "v4.3 --> 推手累计佣金", response = ActivityCardStatResult.class)
    @PostMapping(value = "/stat/commission")
    public AjaxResponseBody<Map<Integer, List<ActivityCardStatResult>>> getCommissionStat(String province, String city) {
        PushHandPlanStatParam param = new PushHandPlanStatParam();
        param.setCity(city);
        param.setProvince(province);
        param.setLevel(1);
        List<ActivityCardStatResult> oneResults = userMakerService.getCommissionStat(param);
        param.setLevel(2);
        List<ActivityCardStatResult> twoResults = userMakerService.getCommissionStat(param);
        param.setLevel(3);
        List<ActivityCardStatResult> threeResults = userMakerService.getCommissionStat(param);
        return AjaxResponseBody.success(ImmutableMap.of(1, oneResults, 2, twoResults, 3, threeResults));
    }

    @ApiOperation(value = "v4.3 --> 邀请推手统计", notes = "返回结果中1白银，2黄金，3钻石", response = ActivityCardStatResult.class)
    @PostMapping(value = "/stat/people")
    public AjaxResponseBody<Map<Integer, List<ActivityCardStatResult>>> getPeopleStat(String province, String city) {
        PushHandPlanStatParam param = new PushHandPlanStatParam();
        param.setCity(city);
        param.setProvince(province);
        param.setLevel(1);
        List<ActivityCardStatResult> oneResults = userMakerService.getPeopleStat(param);
        param.setLevel(2);
        List<ActivityCardStatResult> twoResults = userMakerService.getPeopleStat(param);
        param.setLevel(3);
        List<ActivityCardStatResult> threeResults = userMakerService.getPeopleStat(param);
        return AjaxResponseBody.success(ImmutableMap.of(1, oneResults, 2, twoResults, 3, threeResults));
    }

    @ApiOperation(value = "v4.3 --> 推手列表", response = PushHandPlanListResult.class)
    @PostMapping(value = "/list")
    public AjaxResponseBody<PageResult<PushHandPlanListResult>> list(PageParam param) {
        PageResult<PushHandPlanListResult> result = userMakerService.pushHandList(param);
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "v4.3 --> 推手列表导出")
    @PostMapping(value = "/list/download")
    public void listDownload(HttpServletResponse response) {
        List<PushHandPlanListResult> results = userMakerService.pushHandListDownload();
        List<PushHandPlanListDownloadResult> resultsDownloads = Lists.newArrayList();
        for (PushHandPlanListResult result : results) {
            PushHandPlanListDownloadResult bean = BeanUtil.toBean(result, PushHandPlanListDownloadResult.class);
            bean.setTotalCommission(FormatUtils.fenToYuan(result.getTotalCommission()));
            resultsDownloads.add(bean);
        }
        ExcelUtil.disposeWriteExcelWithSheets(response, resultsDownloads, new PushHandPlanListDownloadResult());
    }

}
